image_name=$1
printf "镜像名称为:${image_name}"
fastapi_start=$(docker ps -f name=${image_name} |awk 'NR!=1 {print $NF}')
fastapi_no_start=$(docker ps -a -f name=${image_name} |awk 'NR!=1 {print $NF}')
if [ ${#fastapi_start} -ge 1 ]; then
    printf "发现启动中的容器 进行停止与删除操作 \n"
    docker stop ${image_name}
    docker rm ${image_name}
    printf "容器删除完成 \n"
elif [ ${#fastapi_no_start} -ge 1 ]; then
    printf "发现未启动的容器 进行删除操作 \n"
    docker rm ${image_name}
else
  printf "未发现启动中与未启动的容器 \n"
fi
printf "环境初始化完成 开始打包镜像 \n"
if [ ${image_name} == "fast_api" ]; then
  docker build --target=${image_name} -t ${image_name} .
  printf "镜像打包完成 启动容器 \n"
  docker run --restart=always -td -p 8817:8817 -v /data/applogs/fasttest-logs:/fast_api/app/logs -v /etc/localtime:/etc/localtime --name ${image_name} ${image_name}
else
  docker build -f ./runcase_statistics/Dockerfile -t ${image_name} .
  printf "镜像打包完成 启动容器 \n"
  docker run -i -v /etc/localtime:/etc/localtime --name ${image_name} ${image_name}
fi
printf "容器启动完成 删除无效镜像 \n"
docker rmi -f  `docker images | grep '<none>' | awk '{print $3}'`
printf "镜像删除完成 构建流程结束 \n"